#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from sys import platform

import yaml

import os
import re

# 定义 config 目录路径
config_dir = "config"

def getConfigEnvs():
    # 定义正则表达式，匹配文件名中的目标部分
    pattern = re.compile(r"application-(.*?)\.yaml")
    # 保存提取结果的数组
    extracted_names = []
    # 遍历 config 目录
    for filename in os.listdir(config_dir):
        # 匹配文件名
        match = pattern.match(filename)
        if match:
            # 提取目标部分并保存到数组
            extracted_names.append(match.group(1))
    return extracted_names

def commonConfig():
    current_directory = os.getcwd()
    # env值及公共配置
    common_config_path = os.path.join(current_directory, 'config', 'application.yaml')
    if os.path.exists(common_config_path): 
        with open(common_config_path, 'r', encoding='utf-8') as f:
            cfg = yaml.safe_load(f)
    else:
        print("{},未找到主配置！".format(common_config_path))
        return {}
    # 主要参数配置
    return cfg
    
def config(env="hkf"):
    current_directory = os.getcwd()
    config_path = os.path.join(current_directory, 'config', 'application-{}.yaml'.format(env))
    if os.path.exists(config_path): 
        with open(config_path, 'r', encoding='utf-8') as f:
            cfg = yaml.safe_load(f)
            mapping = cfg.get("mapping")
    else:
        print("{},未找到分配置！".format(config_path))
        return {}
    # yaml配置优先
    return env,cfg["login"], mapping, { str(index):item for index, item in enumerate(mapping.keys(), start=1) }


# env,login_info, column_mapping, platforms = config()
